package IHM;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import Utils.Parametres;



public class pCreateBillard extends JPanel{
	public static int idoBande = 0;
	public static int idoClearBandes = -2;
	public static int idoClearLastBande = -3;

	public pCreateBillard(){
		setLayout(new BorderLayout(0, 0));
		pView pview = new pView();
		pOutils poutils = new pOutils(pview);
		add(pview,BorderLayout.CENTER);
		add(poutils,BorderLayout.EAST);
		pview.setPOutils(poutils);
		
		new btnOutils("Clear",poutils,idoClearBandes);
		new btnOutils("Effacer dernier",poutils,idoClearLastBande);
		new btnOutils("Bande",poutils,idoBande);

	}
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					JFrame frame = new JFrame();

					frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
					frame.setBounds(100, 100, 450, 300);
					pCreateBillard contentPane = new pCreateBillard();
					frame.setContentPane(contentPane);

					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});

	}
}

class pView extends JPanel{
	pOutils poutils;

	ArrayList<int[][]> listeBandes=new ArrayList<int[][]>();

	public pView(){
		Parametres.parametresPanels0(this);

		addMouseListener(new MouseAdapter(){

			@Override
			public void mouseClicked(MouseEvent e) {

			}

			@Override
			public void mouseEntered(MouseEvent e) {

			}

			@Override
			public void mouseExited(MouseEvent e) {

			}

			@Override
			public void mousePressed(MouseEvent e) {
				if(poutils.getOutil()==pCreateBillard.idoBande){
					int[] x0y0 = {e.getX(),e.getY()};
					int[][] xyxy = new int[2][2];
					xyxy[0]=x0y0;
					listeBandes.add(xyxy);
				}
			}

			@Override
			public void mouseReleased(MouseEvent e) {
				if(poutils.getOutil()==pCreateBillard.idoBande){
					int[][] xyxy = listeBandes.remove(listeBandes.size()-1);

					for(int[][] xyxy2 : listeBandes){
						int d1 = (xyxy[0][0]-xyxy2[0][0])*(xyxy[0][0]-xyxy2[0][0])+(xyxy[0][1]-xyxy2[0][1])*(xyxy[0][1]-xyxy2[0][1]);
						int d2 = (xyxy[0][0]-xyxy2[1][0])*(xyxy[0][0]-xyxy2[1][0])+(xyxy[0][1]-xyxy2[1][1])*(xyxy[0][1]-xyxy2[1][1]);
						int d3 = (xyxy[1][0]-xyxy2[0][0])*(xyxy[1][0]-xyxy2[0][0])+(xyxy[1][1]-xyxy2[0][1])*(xyxy[1][1]-xyxy2[0][1]);
						int d4 = (xyxy[1][0]-xyxy2[1][0])*(xyxy[1][0]-xyxy2[1][0])+(xyxy[1][1]-xyxy2[1][1])*(xyxy[1][1]-xyxy2[1][1]);

						if(d1<400)xyxy[0]=xyxy2[0];
						if(d2<400)xyxy[0]=xyxy2[1];
						if(d3<400)xyxy[1]=xyxy2[0];
						if(d4<400)xyxy[1]=xyxy2[1];
					}
					if(xyxy[0]!=xyxy[1])listeBandes.add(xyxy);
					repaint();
				}

			}

		});
		addMouseMotionListener(new MouseMotionListener() {

			@Override
			public void mouseMoved(MouseEvent arg0) {

			}

			@Override
			public void mouseDragged(MouseEvent e) {
				if(poutils.getOutil()==pCreateBillard.idoBande){
					int[] x1y1 = {e.getX(),e.getY()};
					int[][] xyxy = new int[2][2];
					xyxy = listeBandes.remove(listeBandes.size()-1);
					xyxy[1]=x1y1;
					listeBandes.add(xyxy);

					repaint();
				}
			}
		});
	}
	public void setPOutils(pOutils poutils){
		this.poutils = poutils;
	}
	public void clearBandes(){
		listeBandes.clear();
		repaint();
	}
	public void clearLastBande(){
		if(listeBandes.size()!=0){
			listeBandes.remove(listeBandes.size()-1);
			repaint();
		}
	}

	public void paintComponent(Graphics g) {
		super.paintComponent(g);
		g.setColor(Color.BLUE);
		for(int[][] xyxy : listeBandes){
			g.drawLine(xyxy[0][0], xyxy[0][1], xyxy[1][0], xyxy[1][1]);
		}
	}
}

class pOutils extends JPanel{
	int outilId=-1;
	pView pview;
	public pOutils(pView pview){
		this.setLayout(new GridLayout(0, 1, 0, 0));
		this.pview = pview;
	}
	public int getOutil(){
		return outilId;
	}
	public void setOutil(int id){
		outilId = id ;
	}
	public pView getPView(){
		return pview;
	}
}

class btnOutils extends JButton implements ActionListener{

	int id;
	pOutils poutils;

	public btnOutils(String label,pOutils poutils,int id){
		super(label);
		this.id=id;
		this.poutils=poutils;
		this.addActionListener(this);
		poutils.add(this);
	}

	public void actionPerformed(ActionEvent e) {
		if(id<0){
			if(id==-2)poutils.getPView().clearBandes();
			else if(id==-3)poutils.getPView().clearLastBande();
		}
		else poutils.setOutil(id);	
	}
}
